package servlets;

import Utils.Product;
import Utils.dbManager;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author aleX
 */
@WebServlet(name="SellerProdottiSearch", urlPatterns={"/SellerProdottiSearch"})
public class SellerProdottiSearch extends HttpServlet {
    private dbManager manager;

  @Override
public void init() throws ServletException {
// inizializza il DBManager dagli attributi di Application
this.manager = (dbManager)super.getServletContext().getAttribute("dbmanager");
} 

    
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
     
            
       
           
      try {
    // TODO: usare un Filter per controllare se esiste un utente
    //collegato e/o la sessione è scaduta, altrimenti rimandare al login
HttpSession session = request.getSession(false);
        if(session.getAttribute("seller")==null)
            response.sendRedirect(request.getContextPath() + "/ErrorLoginServlet");
        
String username=(String)session.getAttribute("username");

    if (session == null) {
    response.sendRedirect(request.getContextPath()+ "/ErrorLoginServlet");
    } else {
    List<Product> product = manager.getProductsSeller(username);
    session.setAttribute("products", product);
    
 
    
    //EMETTERE il CODICE HTML della pagine qui
     response.setContentType("text/html");
            PrintWriter out = response.getWriter();
            String docType=
             "<!doctype html public \"-//w3c//dtd html 4.0 transitional//en\">\n";    
    out.println(docType+"<html>");
    out.println("<head>"
    + "<link rel=\"stylesheet\" type=\"text/css\" href=\"css/bootstrap.css\"> ");
    out.println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\">");
    out.println("<title>LANDINGBUYER Page</title>");
    out.println("<meta name=\"generator\" content=\"WYSIWYG Web Builder 8 - http://www.wysiwygwebbuilder.com\">");
    out.println("<style type=\"text/css\">");
    out.println("</style>");
    out.println("</head>");
    out.println("<body>");
    out.println("<div class=\"navbar  navbar-inverse nvbar-fixed-top\">" +
    "<div class=\"navbar-inner\">" +
    "<div class=\"container\">" +
     

    "<a class=\"btn btn-navbar navbar-inverse nvbar-fixed-top \" data-toggle=\"collapse\" data-target=\".nav-collapse\">" +
    "<span class=\"icon-bar\"></span>" +
    "<span class=\"icon-bar\"></span>" +
    "<span class=\"icon-bar\"></span>" +
    "</a>" +
     


               " <ul class=\"nav\">" +
       " <li><a href=\"#\"></a></li>" +
       " <li class=\"active\">" +
       " <a href=\"#\">Prodotti in Vendita</a>" +
       " </li>" +     
       " <li><a href=\"http://localhost:8084/PROGETTO_WEB_1/SellerLandingServlet\">Seller Page</a></li>"+
       " <li><a href=\"http://localhost:8084/PROGETTO_WEB_1/LogoutServlet\">Log Out</a></li>" +
       " </ul>" +

    "<div class=\"nav-collapse collapse\">" +

    "</div>" +
     
    "</div>" +
    "</div> </div>");

    out.println("<div id=\"wb_Text1\" style=\"position:absolute;left:84px;top:64px;\"> ");
    out.println("<h2>Products</h2> </div>");

    out.println("  <table id=\"selectable\" class=\"table table-bordered table-hover\" style=\"position:absolute;left:84px;top:200px;width:400px;\">\n" +
    "        <tr>\n" +
        
    "            <th>\n" +
    "                Product\n" +
    "            </th>   \n" +
    "            <th>\n" +
    "                Price\n" +
    "            </th>\n" +
                 "<th>\n" +
    "                Um\n" +
    "            </th>\n" +
                 "<th>\n" +
    "                Quantity\n" +
    "            </th>\n" +
                 "<th>\n" +
    "                Category\n" +
    "            </th>\n" +
                 "<th>\n" +
    "                Id_Product\n" +
    "            </th>\n" +
                 
    "        </tr>\n" );
 
  for (int i = 0; i < product.size(); i++) {

    
    String p= product.get(i).getProduct();
   float f= product.get(i).getPrice();
   String u= product.get(i).getUm();
   String c= product.get(i).getCategory();
   String id= product.get(i).getUsername();
   int q= product.get(i).getQuantity();
   int idp=product.get(i).getId_Product();
    out.println( 
      
            "        <tr>\n"+
                     
            "            <td>\n" +p+ 
            "            <td>\n" +f+
            "            <td>\n" +u+
            "            <td>\n" +q+
            "            <td>\n" +c+ 
            "            <td>\n" +idp+
  
            "            </td>\n" +
            "</tr>");}
        
   out.println( "    </table><br>");

    out.println("</body>");
    out.println("</html>");
     

  }
    } catch (SQLException ex) {   
         throw new ServletException(ex);
    }
 
            
               
    
    }

    
}